<template>
	<view>
		<!-- 搜索框 -->
		<my-search bgcolor="#c00000" radius="18" @handleclick="gotoSearch"></my-search>
		<!-- 分类选择 -->
		<view class="cate-container">
			<!-- 左侧一级分类 -->
			<scroll-view class="cate-left" :style="{height: wh+'px'}" scroll-y>
				<view :class=" ['cate-left-item', active===index?'active':'']" v-for="(item,index) in cateList"
					:key="item.cat_id" @click="changeActive(index)">
					{{ item.cat_name }}
				</view>
			</scroll-view>
			<!-- 右侧二级分类 -->
			<scroll-view class="cate-right" :style="{height:wh+'px'}" scroll-y :scroll-top="scroll">

				<view class="cate-right-item" v-for="(item,index) in cateLevel2" :key="index">
					<!-- 二级分类标题 -->
					<view class="cate-right-item-title">/{{item.cat_name}}/</view>
					<!-- 二级分类具体内容 -->
					<view class="cate-right-item-content">
						<view class="cate-right-item-content-box" v-for="(v,i) in item.children" :key="i"
							@click="gotoGoodsList(v)">
							<image :src="v.cat_icon" mode="widthFix"></image>
							<text>{{ v.cat_name }}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],
		data() {
			return {
				cateList: [], // 左侧菜单数据
				cateLevel2: [], // 二级分类数据
				active: 0, // 给对应的添加类名
				wh: 0, // 左侧适配高度设置
				scroll: 0 // 滚动条距离顶部的值
			};
		},
		onLoad() {
			// 获取屏幕可用高度
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight
			this.getCateList()
		},
		methods: {
			// 获取分类数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.cateList = res.message
				this.cateLevel2 = this.cateList[0].children
			},
			// 动态active类
			changeActive(index) {
				this.active = index
				this.cateLevel2 = this.cateList[index].children
				this.scroll = Math.random()
			},
			// 点击商品分类 进行分类页跳转
			gotoGoodsList(cate) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${cate.cat_id}`
				})
			},
			// 点击搜索
			gotoSearch() {
				// console.log('点击了搜索框')
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;

			.cate-left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 120rpx;
				background-color: #efefef;
				font-size: 24rpx;

				// 选中样式
				&.active {
					display: flex;
					justify-content: space-between;
					background-color: #ffffff;

					// 左边红线
					&::before {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
					}

					// 右边白线和背景相同
					&::after {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #fffff;
					}
				}
			}
		}

		// 二级分类盒子
		.cate-right {
			background-color: white;

			.cate-right-item {
				.cate-right-item-title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;

					.cate-right-item-content-box {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 33.33%;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
